package com.heima.wemedia.controller.v1;

import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 姜星宇
 * @date 2024/8/27 15:46
 */
@Api(value = "/api/v1/news", tags = {""})
@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController {

    @Resource
    private WmNewsService wmNewsService;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "WmNewsPageReqDto", name = "dto", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("list")
    public ResponseResult<List<WmNews>> findAll(@RequestBody WmNewsPageReqDto dto){
        return wmNewsService.findAll(dto);

    }


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "WmNewsDto", name = "dto", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/submit")
    public ResponseResult<Object> submitNews(@RequestBody WmNewsDto dto) {
        return wmNewsService.submitNews(dto);
    }

    /**
     *  查看详情
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "查看详情", notes = "查看详情", httpMethod = "GET")
    @GetMapping("/one/{id}")
    public ResponseResult<WmNews> findOne(@PathVariable Integer id) {
        WmNews wmNews = this.wmNewsService.getById(id);
        if(wmNews == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }else{
            return ResponseResult.okResult(wmNews);
        }
    }

    /**
     * 文章删除
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "文章删除", notes = "文章删除", httpMethod = "GET")
    @GetMapping("/del_news/{id}")
    public ResponseResult<Object> delNews(@PathVariable Integer id) {
        return this.wmNewsService.getdelNews(id);
    }

    /**
     * 文章上下架
     */

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "WmNewsDto", name = "wmNewsDto", value = "", required = true)
    })
    @ApiOperation(value = "文章上下架", notes = "文章上下架", httpMethod = "POST")
    @PostMapping("/down_or_up")
    public ResponseResult<Object> downOrUp(@RequestBody WmNewsDto wmNewsDto) {
        return this.wmNewsService.downOrUp(wmNewsDto);
    }




}
